<script setup>
import { reactive, watch } from 'vue'

const props = defineProps({
  from: {
    type: Object,
    default: () => []
  },
})

const emit = defineEmits(['change'])


const baseFormData = reactive({})

watch(
  () => props.from,
  (newFrom) => {
    newFrom.forEach(item => {
      baseFormData[item.fieldName] =
        item.defaultValue !== undefined
          ? item.defaultValue
          : item.component === "Select"
            ? null
            : item.component === "Checkbox"
              ? false
              : "";

    })
  },
  { deep: true, immediate: true }
)

watch(
  () => baseFormData,
  (newData) => {
    emit('change', newData)
  },
  { deep: true }
)




</script>
<template>
  <el-form :inline="true" class="mb-2 ml-6 flex justify-between gap-1" size="small">
    <div class="flex gap-2">
      <el-form-item v-for="item in from" class="flex-row-reverse gap-2 justify-center" :label="item.label"
        :key="item.fieldName">
        <div class="flex items-center gap-1" v-if="item.component === 'Checkbox'">
          <component v-if="typeof item.prefix === 'function'" :is="item.prefix()" />
          <span v-else-if="typeof item.prefix === 'string'">
            {{ item.prefix }}
          </span>
          <el-checkbox v-model="baseFormData[item.fieldName]" v-bind="item.componentProps" />
          <component v-if="typeof item.suffix === 'function'" :is="item.suffix()" />
          <span v-else-if="typeof item.suffix === 'string'">
            {{ item.suffix }}
          </span>
        </div>
        <div class="flex items-center gap-1" v-if="item.component === 'Input'">
          <component v-if="typeof item.prefix === 'function'" :is="item.prefix()" />
          <span v-else-if="typeof item.prefix === 'string'">
            {{ item.prefix }}
          </span>
          <el-input v-model="baseFormData[item.fieldName]" v-bind="item.componentProps" />
          <component v-if="typeof item.suffix === 'function'" :is="item.suffix()" />
          <span v-else-if="typeof item.suffix === 'string'">
            {{ item.suffix }}
          </span>
        </div>
        <div class="flex items-center gap-1" v-if="item.component === 'Select'">
          <component v-if="typeof item.prefix === 'function'" :is="item.prefix()" />
          <span v-else-if="typeof item.prefix === 'string'">
            {{ item.prefix }}
          </span>
          <el-select v-model="baseFormData[item.fieldName]" v-bind="item.componentProps">
            <el-option v-for="option in item.componentProps.options" :key="option.value" :label="option.label"
              :value="option.value" />
          </el-select>
          <component v-if="typeof item.suffix === 'function'" :is="item.suffix()" />
          <span v-else-if="typeof item.suffix === 'string'">
            {{ item.suffix }}
          </span>
        </div>
      </el-form-item>
    </div>
    <div>
      <slot name="form-btn" :row="baseFormData" />
    </div>
  </el-form>
</template>

<style scoped lang="scss">
:deep(.el-form-item) {
  margin-right: 0;
  align-items: center
}

:deep(.el-checkbox) {
  margin-right: 0;
}
</style>
